# -*- coding: utf-8 -*-
#
#
##
class WebController < ApplicationController

  def info
    #画像情報取得
   begin
    user_id =  params[:user_id].to_i
    raise ParameterException, "User id is invalid : #{user_id}" unless user_id > 0
    
    pot_id = params[:pot_id].to_i
    raise ParameterException, "Pot id is invalid : #{pot_id}" unless pot_id >= 1 && pot_id <= 5
    
    usr = User.find(user_id)

   item_num  =  usr.read_attribute("p#{pot_id}_item_num".to_sym)
   special_num = usr.read_attribute("p#{pot_id}_special_num".to_sym)
   complete_pattern = usr.read_attribute("p#{pot_id}_complete_pattern".to_sym)
   
   if item_num !=0

   @image_url = "http://bakohead.info/img/#{pot_id}_#{item_num}_#{complete_pattern}_#{special_num}.png"
   @card_url = "http://bakohead.info/images/card/meishi_#{pot_id}.gif"  #誰がどの名刺か確認

   else
    @image_url = "http://bakohead.info/img/q.gif"
    @card_url =  "http://bakohead.info/img/q.gif" #名刺の?画像つくってもらう。
   end
   
    #商品情報を取得
    potitems = Potitem.find(:all, :conditions => ["userid = ? and potid = ?", user_id , pot_id ])

    #itemname imgurl weburl が引けるハッシュを作成
    hash = Hash.new
		@array = Array.new

    potitems.each do |potitem|
      rakutenitems = Rakutenitem.find(:all, :conditions => ["potid = ? and barcode = ? ", potitem.potid , potitem.barcode ])
      rakutenitems.each do |items|
        hash = { 'itemname' => items.itemname , 'weburl' => items.weburl , 'imageurl' => items.imageurl }
      end
      @array.push(hash)
    end

    #で上記の@arrayを view側で以下のような感じにしてハッシュの中身を出してやる？
    # @array.each do |i|
    #  puts i['itemname']
    # end

    
    rescue ActiveRecord::RecordNotFound
      render :action => 'error'
    rescue ParameterException
      render :action => 'error'
    rescue NameError
      render :action => 'error'
   end
       

  end
 
  def about
    
  end

  def itemlist
     begin
    user_id =  params[:user_id].to_i
    raise ParameterException, "User id is invalid : #{user_id}" unless user_id > 0

    pot_id = params[:pot_id].to_i
    raise ParameterException, "Pot id is invalid : #{pot_id}" unless pot_id >= 1 && pot_id <= 5
    
    #商品情報を取得
    potitems = Potitem.find(:all, :conditions => ["userid = ? and potid = ?", user_id , pot_id ])

    #itemname imgurl weburl が引けるハッシュを作成
    hash = Hash.new
		@array = Array.new

    potitems.each do |potitem|
      rakutenitems = Rakutenitem.find(:all, :conditions => ["potid = ? and barcode = ? ", potitem.potid , potitem.barcode ])
      rakutenitems.each do |items|
        hash = { 'itemname' => items.itemname , 'weburl' => items.weburl , 'imageurl' => items.imageurl }
      end
      @array.push(hash)
    end

    #で上記の@arrayを view側で以下のような感じにしてハッシュの中身を出してやる？
    # @array.each do |i|
    #  puts i['itemname']
    # end


    rescue ActiveRecord::RecordNotFound
      render :action => 'error'
    rescue ParameterException
      render :action => 'error'
    rescue NameError
      render :action => 'error'
   end
  end



end
